package com.muki.extractor;

import java.util.List;
import java.util.concurrent.CountDownLatch;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.muki.contentSeen.Content;

/**
 * 信息抓取服务器
 * @author ljh_2017
 *
 */
@Component
@EnableScheduling
public class ExtractorServer {

	//数据集合
	@Autowired private ExtractorService extractorService;
	@Autowired private ExtractorBeen been;

	private List<Content> contents = null;
	private static final Logger log = Logger.getLogger(ExtractorServer.class);


	@Scheduled(fixedRate=10*1000)
	public void execute() {
		log.info("正在加载...");
		if(contents==null || contents.isEmpty()) {
			loadDataSource();
		}
	}

	private void loadDataSource() {
		contents = extractorService.getUnExtractContents();
		if(contents.isEmpty()) {
			return ;
		}
		CountDownLatch latch = new CountDownLatch(contents.size());
		for(Content content:contents) {
			if(!content.isStatus()) {
				try {
					been.doTask(content,latch);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		try {
			log.info("解析任务执行完毕");
			latch.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		for(Content c:contents) {
			c.setStatus(true);
			extractorService.updateContent(c);
			log.info("网页抓取完毕，正在更新抓取状态:"+c.getUrl());
		} 
		log.info("数据加载完毕...正在访问缓冲区");
		contents.clear();
	}
}
